
//
//  YuanJiangScanImage.m
//  YJScanImageDemo
//
//  Created by 申元江 on 16/10/27.
//  Copyright © 2016年 YuanJiang. All rights reserved.
//

#import "YuanJiangScanImage.h"

@implementation YuanJiangScanImage

static CGRect oldFrame;

+(void)scanBigImageWithImageView:(UIImageView *)currentImageView
{
    UIImage *image = currentImageView.image;
    //当前视图**
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    //背景
    UIView *backgroundView= [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    //当前imageview的原始尺寸->将像素currentImageview.bounds由currentImageview.bounds所在视图转换到目标视图window中，返回在目标视图window中的像素值
    //rect 对应到 bounds
    oldFrame = [currentImageView convertRect:currentImageView.bounds toView:window];
    [backgroundView setBackgroundColor:[UIColor colorWithRed:107/255.0 green:107/255.0 blue:99/255.0 alpha:0.6]];
    //此视图不会显示
    backgroundView.alpha=0;
    //将所展示的View添加到背景视图中
    UIImageView *imageView =[[UIImageView alloc]initWithFrame:oldFrame];
    [imageView setImage:image];
    [imageView setTag:0];
    [backgroundView addSubview:imageView];
    [window addSubview:backgroundView];
    
     //点击事件使背景View消失
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hiddenImageView:)];
    [backgroundView addGestureRecognizer:tap];
    
    //动画放所展示的imageView
    [UIView animateWithDuration:0.4 animations:^{
        CGFloat y,width,height;
        y = ([UIScreen mainScreen].bounds.size.height-image.size.height*[UIScreen mainScreen].bounds.size.width/image.size.width)*0.5;
        //宽为图片的宽
        width = [UIScreen mainScreen].bounds.size.width;
        height = [UIScreen mainScreen].bounds.size.width/image.size.width*image.size.height;
        [imageView setFrame:CGRectMake(0, y, width, height)];
        backgroundView.alpha = 1;
        
    } completion:^(BOOL finished) {
    }];
    
}
//隐藏
+(void)hiddenImageView:(UITapGestureRecognizer *)tap
{
    UIView *backgroundView = tap.view;
    //原始的imageView
    UIImageView *imageView = [tap.view viewWithTag:0];
    //回复
    [UIView animateWithDuration:0.4 animations:^{
        [imageView setFrame:oldFrame];
        [backgroundView setAlpha:0];
    } completion:^(BOOL finished) {
        //完成后将背景视图删掉
        [backgroundView removeFromSuperview];
        
    }];

}

@end
